/**
 * @author 钟一华
 */
var sysId;
var parentId;
var dialogId;
//使目标指标信息的必保值和挑战值可以编辑
$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var fields = $("#orderDatagird").datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $("#orderDatagird").datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
				}
			}
			$("#orderDatagird").datagrid('beginEdit', param.index);
			for(var i=0; i<fields.length; i++){
				var col = $("#orderDatagird").datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	}
});
//点击grid单元格事件
function onClickCell(index, field, value){
	if (endEditing()){
		if(field == 'nodeSortNumber'){
			$('#orderDatagird').datagrid('selectRow', index).datagrid('editCell', {index:index,field:field});
			editIndex = index;
		}
	}
}
//编辑单元格
var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){
		return true;
	}
	if ($('#orderDatagird').datagrid('validateRow', editIndex)){
		$('#orderDatagird').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
//保存
function saveData() {
	var flag = $('#orderDatagird').datagrid('validateRow', editIndex);
	if (flag) {
		$('#orderDatagird').datagrid('endEdit', editIndex);
		var rows = $('#orderDatagird').datagrid('getChanges');
		if (rows.length > 0) {
			var datas = JSON.stringify(rows);
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: 'platform/archiveclassmanagerController/updateOrderData',
				data: {
					datas: datas,
					sysId: sysId
				},
				success: function(result) {
					if (result.flag == 0) {
						parent.$.messager.show({
							title: '消息',
							//commonLang.optip.save.success替换'保存成功!'
							msg: commonLang.optip.save.success,
							timeout: 3000,
							showType: 'slide'
						});
						parent.reloadTree(parentId);
						parent.$('#' + dialogId).dialog('close');
					} else {
						$.messager.show({
							title: '消息',
							//commonLang.optip.save.fail替换'保存失败!'
							msg: commonLang.optip.save.fail,
							timeout: 3000,
							showType: 'slide'
						});
					}
				}
			});
		} else {
			//commonLang.tip.alert替换'提示框',commonLang.optip.save.noChangeData替换'没有变更的数据可以进行保存操作！'
			$.messager.alert(commonLang.tip.alert, commonLang.optip.save.noChangeData, 'warning');
			return;
		}
	} else {
		//commonLang.tip.alert替换'提示框',commonLang.optip.save.importNum替换'请输入0-15位的正整数！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.save.importNum, 'warning');
		return;
	}
}
/**
 * 关闭弹出窗口
 */
function closeForm() {
	if (parent != null && parent.$('#' + dialogId) != null) {
		parent.$('#' + dialogId).dialog('close');
	}
}